package com.mgl.movesprite;

import java.util.ArrayList;

import org.andengine.engine.camera.hud.HUD;

import com.mgl.movesprite.game.factory.GameObjectFactory;
import com.mgl.movesprite.game.factory.GameType;
import com.mgl.movesprite.game.model.Level;
import com.mgl.movesprite.game.model.Tower;
import com.mgl.util.ButtonSprite;
import com.mgl.util.ButtonSpriteListener;
import com.mgl.util.CircleList;
import com.mgl.util.Point;

public class BuildPanel {

	private Level level = null;

	private EntitySpriteLevelBase background;
	private CloseButton closeButton;

	private CircleList circleList = null;
	private EntitySpriteLevelBase itemToShow = null;
	private EntitySpriteLevelBase itemToBuild = null;
	private EntitySpriteLevelBase itemToBuild1 = null;

	private Point pointToBuild = null;

	private ButtonSprite nextButton = null;
	private ButtonSprite prevButton = null;

	public BuildPanel(Level level) {
		this.level = level;
		background = (EntitySpriteLevelBase) level.getGameObjectFactory().createObject(
				GameType.PANEL_BACKGROUND, 0, 0);
		closeButton = (CloseButton) level.getGameObjectFactory().createObject(
				GameType.CLOSE_BUTTON, 0, 0);

		closeButton.setPanel(this);

		initButtons();

	}

	public void show( ArrayList<GameType> typeBuildList, EntitySpriteLevelBase buildPlace) {
		try {
			HUD hud = level.getHud();

			background.setPosition(0, level.getCamera().getHeight() - 100);
			closeButton.setPosition(level.getCamera().getWidth() - 75,
					level.getCamera().getHeight() - 75);

			nextButton.setPosition(200, level.getCamera().getHeight() - 75);
			prevButton.setPosition(30, level.getCamera().getHeight() - 75);

			hud.attachChild(background);
			hud.attachChild(closeButton);
			hud.attachChild(nextButton);
			hud.attachChild(prevButton);

			hud.registerTouchArea(closeButton);
			hud.registerTouchArea(nextButton);
			hud.registerTouchArea(prevButton);

			initCircleList(typeBuildList);

			if (!circleList.isEmpty()) {

				itemToShow = (EntitySpriteLevelBase) level.getGameObjectFactory().createObject(
						circleList.getFirst(), 0, 0);
				itemToShow.setPosition(100, level.getCamera().getHeight() - 80);
				hud.attachChild(itemToShow);
				hud.registerTouchArea(itemToShow);

			}
			pointToBuild = buildPlace.getCenterPoint();
			pointToBuild = new Point(buildPlace.getX()+pointToBuild.getX(), buildPlace.getY()+pointToBuild.getY());
			
			if(itemToShow!=null){
				setListenerToItemToshow();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void close() {
		try {
	
			if (level.getHud() == null) {
				System.out.println("BuildPanel, el hud es null");
				return;
			}
	
			level.getHud().detachChild(background);
			level.getHud().detachChild(closeButton);
			level.getHud().detachChild(nextButton);
			level.getHud().detachChild(prevButton);
	
			if (itemToShow != null) {
				level.getHud().detachChild(itemToShow);
				itemToShow = null;
			}
			
			if(itemToBuild!=null){
				level.getScene().detachChild(itemToBuild);
			}
	
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}

	private void initCircleList(ArrayList<GameType> typeBuildList) {
		try {

			circleList = new CircleList();

			for (GameType type : typeBuildList) {

				circleList.add(type);

			}
			System.out.println("El tamanio de circle list es : "
					+ circleList.size());

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void setListenerToItemToshow() {
		itemToShow.setButtonSpriteListener(new ButtonSpriteListener() {

			@Override
			public void onTouchUp() {
				
			}

			@Override
			public void onTouchMove() {

			}

			@Override
			public void onTouchDown() {
				if(itemToBuild!=null && itemToBuild.hasParent()){
					level.getScene().detachChild(itemToBuild);
				}
				itemToBuild = (EntitySpriteLevelBase) level.getGameObjectFactory().createObject(itemToShow.getType(), pointToBuild.getX(), pointToBuild.getY());
				itemToBuild.setAlpha((float) 0.5);
				
				 if (itemToBuild instanceof Tower) {
					Tower tower = (Tower) itemToBuild;
					tower.setVisibleRadio(true);
				}
				 
				level.getScene().attachChild(itemToBuild);
				//itemToBuild.attachToScene(level.getScene());
				
			
			}
		});

	}

	private void initButtons() {
		try {

			nextButton = (ButtonSprite) level.getGameObjectFactory()
					.createObject(GameType.NEXT_BUTTON, 0, 0);
			prevButton = (ButtonSprite) level.getGameObjectFactory()
					.createObject(GameType.PREV_BUTTON, 0, 0);

			nextButton.setButtonSpriteListener(new ButtonSpriteListener() {

				@Override
				public void onTouchUp() {

					if (itemToShow != null) {
						level.getHud().detachChild(itemToShow);
					}
					if (!circleList.isEmpty()) {
						itemToShow = (EntitySpriteLevelBase) level.getGameObjectFactory().createObject(
								circleList.getNext(), 0, 0);

						itemToShow.setPosition(100, level.getCamera()
								.getHeight() - 80);
						level.getHud().attachChild(itemToShow);
						level.getHud().registerTouchArea(itemToShow);
					}
				}

				@Override
				public void onTouchMove() {
					// TODO Auto-generated method stub

				}

				@Override
				public void onTouchDown() {
					// TODO Auto-generated method stub

				}
			});

			prevButton.setButtonSpriteListener(new ButtonSpriteListener() {

				@Override
				public void onTouchUp() {
					if (itemToShow != null) {
						level.getHud().detachChild(itemToShow);
					}
					if (!circleList.isEmpty()) {
						itemToShow = (EntitySpriteLevelBase) level.getGameObjectFactory().createObject(
								circleList.getPrev(), 0, 0);

						itemToShow.setPosition(100, level.getCamera()
								.getHeight() - 80);
						level.getHud().attachChild(itemToShow);
						level.getHud().registerTouchArea(itemToShow);
					}

				}

				@Override
				public void onTouchMove() {
					// TODO Auto-generated method stub

				}

				@Override
				public void onTouchDown() {
					// TODO Auto-generated method stub

				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
